package MT::Template::Layouts::TestLayouts;

use MT::Template::Context;
use MT::Template::Layouts;

my $test_layout_01 =
  '<$MTSetVar name="test" value="Hello World"$><$MTGetVar name="test"$>';
my $test_layout_10 =
  '<MTInvoke><MTInvokeTag><$MTGetVar name="TAG"$></MTInvokeTag></MTInvoke>';
my $test_layout_11 =
'<MTInvoke tag="TestLoop"><MTInvokeContents>|<$MTTestLoopItem$></MTInvokeContents></MTInvoke>';
my $test_layout_12 =
'<MTInvoke tag="MTGetVar"><MTInvokeSetArg name="name"><$MTGetVar name="SET"$></MTInvokeSetArg></MTInvoke>';
my $test_layout_13 =
'<MTInvoke tag="MTSetVarBlock" name="TEST"><MTInvokeContents><$MTLayoutArgument name="msg"$></MTInvokeContents></MTInvoke><$MTGetVar name="TEST"$>';
my $test_layout_if_eq =
'<MTInvoke tag="MTSetVarBlock" name="VALUE"><MTInvokeContents><MTIfLayoutArgument name="test" eq="1">TRUE<MTElse>FALSE</MTElse></MTIfLayoutArgument></MTInvokeContents></MTInvoke>';
my $test_layout_if_ne =
'<MTInvoke tag="MTSetVarBlock" name="VALUE"><MTInvokeContents><MTIfLayoutArgument name="test" ne="1">TRUE<MTElse>FALSE</MTElse></MTIfLayoutArgument></MTInvokeContents></MTInvoke>';
my $test_layout_if_lt =
'<MTInvoke tag="MTSetVarBlock" name="VALUE"><MTInvokeContents><MTIfLayoutArgument name="test" lt="1">TRUE<MTElse>FALSE</MTElse></MTIfLayoutArgument></MTInvokeContents></MTInvoke>';
my $test_layout_if_gt =
'<MTInvoke tag="MTSetVarBlock" name="VALUE"><MTInvokeContents><MTIfLayoutArgument name="test" gt="1">TRUE<MTElse>FALSE</MTElse></MTIfLayoutArgument></MTInvokeContents></MTInvoke>';
my $test_layout_if_le =
'<MTInvoke tag="MTSetVarBlock" name="VALUE"><MTInvokeContents><MTIfLayoutArgument name="test" le="1">TRUE<MTElse>FALSE</MTElse></MTIfLayoutArgument></MTInvokeContents></MTInvoke>';
my $test_layout_if_ge =
'<MTInvoke tag="MTSetVarBlock" name="VALUE"><MTInvokeContents><MTIfLayoutArgument name="test" ge="1">TRUE<MTElse>FALSE</MTElse></MTIfLayoutArgument></MTInvokeContents></MTInvoke>';
my $test_layout_unless_eq =
'<MTInvoke tag="MTSetVarBlock" name="VALUE"><MTInvokeContents><MTUnlessLayoutArgument name="test" eq="1">FALSE<MTElse>TRUE</MTElse></MTUnlessLayoutArgument></MTInvokeContents></MTInvoke>';
my $test_layout_unless_ne =
'<MTInvoke tag="MTSetVarBlock" name="VALUE"><MTInvokeContents><MTUnlessLayoutArgument name="test" ne="1">FALSE<MTElse>TRUE</MTElse></MTUnlessLayoutArgument></MTInvokeContents></MTInvoke>';
my $test_layout_unless_lt =
'<MTInvoke tag="MTSetVarBlock" name="VALUE"><MTInvokeContents><MTUnlessLayoutArgument name="test" lt="1">FALSE<MTElse>TRUE</MTElse></MTUnlessLayoutArgument></MTInvokeContents></MTInvoke>';
my $test_layout_unless_gt =
'<MTInvoke tag="MTSetVarBlock" name="VALUE"><MTInvokeContents><MTUnlessLayoutArgument name="test" gt="1">FALSE<MTElse>TRUE</MTElse></MTUnlessLayoutArgument></MTInvokeContents></MTInvoke>';
my $test_layout_unless_le =
'<MTInvoke tag="MTSetVarBlock" name="VALUE"><MTInvokeContents><MTUnlessLayoutArgument name="test" le="1">FALSE<MTElse>TRUE</MTElse></MTUnlessLayoutArgument></MTInvokeContents></MTInvoke>';
my $test_layout_unless_ge =
'<MTInvoke tag="MTSetVarBlock" name="VALUE"><MTInvokeContents><MTUnlessLayoutArgument name="test" ge="1">FALSE<MTElse>TRUE</MTElse></MTUnlessLayoutArgument></MTInvokeContents></MTInvoke>';
my $test_layout_20_1 =
'<MTInvoke tag="MTSetVarBlock" name="VALUE"><MTInvokeContents><MTIfLayoutArgument name="test" eq="1"><$MTTestIndirectGet name="TRUE"$><MTElse><$MTTestIndirectGet name="FALSE"$></MTElse></MTIfLayoutArgument></MTInvokeContents></MTInvoke><$MTGetVar upper_case="1" name="VALUE"$>';
my $test_layout_20_2 =
'<MTInvoke tag="GetVar"><MTInvokeSetArg name="name"><$MTLayoutArgument name="name"$></MTInvokeSetArg></MTInvoke>';
my $echo =
'<MTInvoke tag="GetVar"><MTInvokeSetArg name="name"><$MTLayoutArgument name="copy_from"$></MTInvokeSetArg></MTInvoke>';
my $test_layout_21 = <<TMPL;
<MTInvoke tag="MTSetVarBlock">
<MTInvokeSetArg name="name"><\$MTLayoutArgument name="copy_to"\$></MTInvokeSetArg>
<MTInvokeContents><MTInvoke tag="TestEcho" pass_thru="copy_from"></MTInvoke></MTInvokeContents> 
</MTInvoke>
TMPL

MT::Template::Layouts->add_layout(TestLayout01       => $test_layout_01);
MT::Template::Layouts->add_layout(TestLayout10       => $test_layout_10);
MT::Template::Layouts->add_layout(TestLayout11       => $test_layout_11);
MT::Template::Layouts->add_layout(TestLayout12       => $test_layout_12);
MT::Template::Layouts->add_layout(TestLayout13       => $test_layout_13);
MT::Template::Layouts->add_layout(TestLayoutIfEQ     => $test_layout_if_eq);
MT::Template::Layouts->add_layout(TestLayoutIfNE     => $test_layout_if_ne);
MT::Template::Layouts->add_layout(TestLayoutIfLT     => $test_layout_if_lt);
MT::Template::Layouts->add_layout(TestLayoutIfGT     => $test_layout_if_gt);
MT::Template::Layouts->add_layout(TestLayoutIfLE     => $test_layout_if_le);
MT::Template::Layouts->add_layout(TestLayoutIfGE     => $test_layout_if_ge);
MT::Template::Layouts->add_layout(TestLayoutUnlessEQ => $test_layout_unless_eq);
MT::Template::Layouts->add_layout(TestLayoutUnlessNE => $test_layout_unless_ne);
MT::Template::Layouts->add_layout(TestLayoutUnlessLT => $test_layout_unless_lt);
MT::Template::Layouts->add_layout(TestLayoutUnlessGT => $test_layout_unless_gt);
MT::Template::Layouts->add_layout(TestLayoutUnlessLE => $test_layout_unless_le);
MT::Template::Layouts->add_layout(TestLayoutUnlessGE => $test_layout_unless_ge);
MT::Template::Layouts->add_layout(TestLayout20       => $test_layout_20_1);
MT::Template::Layouts->add_layout(TestIndirectGet    => $test_layout_20_2);
MT::Template::Layouts->add_layout(TestEcho           => $echo);
MT::Template::Layouts->add_layout(TestLayout21       => $test_layout_21);

MT::Template::Context->add_container_tag(TestLoop => \&loop);
MT::Template::Context->add_tag(TestLoopItem => \&loop_item);

sub loop {
    my ($ctx, $args, $cond) = @_;
    my $content = '';
    my $builder = $ctx->stash('builder');
    my $tokens  = $ctx->stash('tokens');
    my @items   = @{$ctx->stash('LOOP')};
    for (@items) {
        $ctx->stash('ITEM', $_);
        defined(my $out = $builder->build($ctx, $tokens, $cond))
          or return $ctx->error($builder->errstr);
        $content .= $out;
    }
    $content;
}

sub loop_item { $_[0]->stash('ITEM') }

1;
